var JSER={};
JSER.Data={};
JSER.Model={};

var PAGE_LIMIT=15;

function merge (o1, o2) 
	{
	for (var z in o2) {
		if (o2.hasOwnProperty(z)) {
			o1[z] = o2[z];
		}
	}
	return o1;
};

JSER.App=function(type) {
	
	var that=this;
	
	this.type=type;	
	this.moduleList=[];
	this.storage=new JSER.Data.Storage();	
	
	this.uiManager=null;	
	
	this.add=function(config) {		
		this.moduleList.push(new JSER.Module(this, config));		
	};
	
	this.loadUiManager=function (){
		switch(this.type) {
			case 'desktop': 
				this.uiManager=new JSER.Model.Desktop();
			  break;
		};
	};
	
	this.run=function() {
		for (var i=0; i<this.moduleList.length; i++) {
			this.moduleList[i].render();	
			
			this.loadUiManager();

			this.uiManager.add(this.moduleList[i]);
		}
		
		Ext.QuickTips.init();
		
		this.uiManager.get();
	};
};

JSER.Module=function(container, config) {
	
	var scope=this;
		
	this.container=container;
	this.config=config;
	
	this.xtype='module';
	this.id='module-'+this.config.name;
	this.name=this.config.name;
	this.title=this.config.title;
	this.controller=this.config.controller;
	
	this.record=this.container.storage.getRecord(config.entity);
	this.store=this.container.storage.getStore(config.entity).get();

	this.storeList=[this.store];
		
	this.rootItem=new JSER.Model.Panel(this, {items: this.config.items});	
	
	this.view=null;
	
	this.render=function() {
		this.view=JSER.Model.render(this.rootItem);		
	};	
	
	this.loadEvents=function() {
		this.controller=JSER.Controller(this.container, this);
	};
};

JSER.Controller=function(app, module) {
	
	var that=this;
	
	this.app=app;
	this.module=module;
	
	this.mainForm=Ext.getCmp("Users-panel-form").getForm();
	this.mainGrid=Ext.getCmp("Users-panel-form-grid");
	
	this.buttonSave=Ext.getCmp("Users-panel-button-save");
	
	this.statusBar=Ext.getCmp("Users-panel-statusbar");
	
	this.buttonSave.addListener('click', function() {
		that.saveRecord();
	});
	
	this.buttonNew=Ext.getCmp("Users-panel-button-new");
	this.buttonDelete=Ext.getCmp("Users-panel-button-delete");	
	
	this.bindRecord=function(obj) {	    	 
    	for (var i=0; i<this.mainForm.items.items.length; i++) 
    		obj.set(this.mainForm.items.items[i].name, this.mainForm.items.items[i].getValue());
		
		return obj; 
    };
    
	this.saveRecord=function() {
    	var rec=this.mainGrid.selModel.getSelected();
    	
    	if (rec)
    		this.editRecord(this.bindRecord(rec));
    	else {
    		var recordProtype=this.module.record.get();
    		this.createRecord(this.bindRecord(new recordProtype()));
    	}
    };
    
    this.createRecord=function(rec) {
    	
		if (this.mainForm.isValid()) {
			
			this.state.create.runing();
			
			Ext.Ajax.request({
				url: that.module.store.proxy.url,
				method: "POST",
				params: '{"data": '+Ext.encode(rec.data)+'}',
				headers: {
					'Content-Type': 'application/json; charset=UTF-8'
				},
				success: function(response, request) {
					var resp=Ext.decode(response.responseText);
					
					if (resp.success)
						that.state.create.success(resp.message);
					else
						that.state.create.failure(resp.message);
				}
			});                  			
		} 
		else
			that.state.create.failure("Registro não é válido para ser inserido!");
    };
    
    this.state={
    	create: {    		
    		ready: function() {    	
    	    	that.buttonNew.disable();
    	    	that.buttonDelete.disable();
    	    	
    	    	that.statusBar.setIcon('');
    	    	that.statusBar.setText('Pronto.');
    	    	
    	    	that.mainGrid.selModel.clearSelections();
    	    	that.mainForm.reset();
    	    },
    	    
    	    runing: function(msg) {  
    	    	that.__running(msg || 'Inserindo registro...');
    	    },
    	    
    	    success: function(msg, id) {  
    	    	that.__success(msg || 'Registro inserido com sucesso!');
    	    },
    	    
    	    failure: function(msg) {
    	    	that.__failure(msg || 'Falha ao inserir registro!');
    	    }	
    	}
    };
    
    this.__running=function(msg) {  
    	that.__showMask();
    	that.statusBar.setIcon('x-status-busy');
    	that.statusBar.setText(msg);
    };
    
    this.__success=function(msg) {  
    	that.__hideMask();
    	that.statusBar.setIcon('x-status-valid');
    	that.statusBar.setText(msg);
    	that.module.store.reload();
    };
    
    this.__failure=function(msg) {
    	that.__hideMask();
    	that.statusBar.setIcon('x-status-error');
    	that.statusBar.setText(msg);
    	that.module.store.reload();
    };
    
    this.__showMask=function() {
    	that.mainForm.getEl().mask();
    };	    
    
    this.__hideMask=function() {
    	that.mainForm.getEl().unmask();
    };	
	
	this.gridController=new JSER.Controller.Grid(this.mainGrid);
	
	this.gridController.linkTo(this.mainForm);
};

JSER.Controller.Grid=function(grid) {
	
	var that=this;
	this.grid=grid;
	
	this.linkTo=function(linkedItem) {
		this.grid.selModel.addListener('rowselect', function(sm, row, rec) {
			linkedItem.loadRecord(rec);						
		});		
	};
};

JSER.Data.Record=function(config) {
	
	var scope=this;
	
	this.config=config;
	
	this.name=this.config.name;
	this.title=this.config.title;
	this.controller=this.config.controller;
	
	this.getItems=function() {
		var list=[];
		var obj=null;
		
		for (var i=0; i<config.items.length; i++) {
			obj=new Object();
			
			obj.name=config.items[i].name;
			obj.type=config.items[i].type;
			
			list.push(obj);
		}	
		
		return list;
	};
	
	this.get=function() {
		if (!this.obj)
			this.obj=Ext.data.Record.create(scope.getItems());
		return this.obj;	
	};
};

JSER.Data.Store=function(config) {
	
	this.config			= config;
	
	this.getName=function() {
		if (this.config.name==undefined)
			throw ("Propriedade obrigatória não definida: name.");
		return this.config.name;
	};
	
	this.getId=function() {
		return this.name+'-store';
	};
	
	this.getRestful=function() {
		return (this.config.restful)? this.config.restful : true;
	};
	
	this.getController=function() {
		return (this.config.controller)?
			this.config.controller
			:
			this.config.name+'s';
	};
	
	this.getEntity=function() {
		if (!this.entity) 			
			this.entity=this.config.entity.get();
				
		return this.entity;
	};
	
	this.getReaderFields=function() {
		return (this.config.readerFields)? 
			this.config.readerFields
			:
			this.getEntity().prototype.fields.keys;
	};
	
	this.getProxy=function() {
		return new Ext.data.HttpProxy({
	    	url: this.getController()
	    });
	};
	
	this.getReader=function() {
		return new Ext.data.JsonReader({
	        root: 'data',
	        totalProperty: 'total',
	        idProperty: 'id',
	        fields: this.getReaderFields()
	    }, this.getEntity());
	};
	
	this.getWriter=function() {
		return new Ext.data.JsonWriter({
			encode: false,
			writeAllFields: true
		});
	};	
	
	this.name			= this.getName();
	this.id				= this.getId();
	this.controller		= this.getId();
	this.restful		= this.getRestful();
	this.entity			= this.getEntity();
	this.restful		= this.getRestful();
	this.readerFields	= this.getReaderFields();
	this.proxy			= this.getProxy();
	this.reader			= this.getReader();
	this.writer			= this.getWriter();	
	
	this.get=function() {
		if (!this.obj)
			this.obj=new Ext.data.Store({
				id				: this.id,
				name			: this.name,
				baseParams		: {'start': 0, 'limit': PAGE_LIMIT},
				successProperty	: 'success',
				prettyUrls		: true,
				autoSave		: false,
				autoLoad		: false,
				batch			: false,
				remoteSort		: true,
				restful			: this.restful,
				
				proxy			: this.proxy,		
				reader			: this.reader,		
				writer			: this.writer
			});	
			
		return this.obj;	
	};
};

JSER.Data.Storage=function() {
	
	this.storeList=[];
	this.recordList=[];
	
	this.add=function(config) {		
		var record=new JSER.Data.Record(config);
		
		this.recordList.push(record);
		
		var store=new JSER.Data.Store({
			name: config.name,
			controller: config.controller,
			entity: record,
			readerFields: record.fields
		});
		
		this.storeList.push(store);
	};
	
	this.create=function(record, component) {
		
		var clsRecord=record.get();
		var objRecord=new clsRecord;
		
		var store=new JSER.Data.Store({
			name: 'store-'+component.container.name+'-'+component.name,
			controller: record.controller,
			entity: objRecord,
			readerFields: objRecord.fields.keys
		});
		
		this.storeList.push(store);
		
		return store;
	};
	
	this.getRecord=function(name) {
		for (var i=0; i<this.recordList.length; i++)
			if (this.recordList[i].name==name)
				return this.recordList[i];
		
		return null;
	};
	
	this.getStore=function(name) {
		for (var i=0; i<this.storeList.length; i++)
			if (this.storeList[i].name==name)
				return this.storeList[i];
		
		return null;
	};
};

JSER.Model.nav={
	getApp: function(item) {
		var getContainer=function (obj) {
			if (obj.constructor==JSER.App)
				return obj;				
			else if (obj.container)
				return getContainer(obj.container);			
		};
		
		return getContainer(item);	
	},
	getModule: function(item) {
		var getContainer=function (obj) {
			if (obj.constructor==JSER.Module)
				return obj;				
			else if (obj.container)
				return getContainer(obj.container);			
		};
		
		return getContainer(item);
	},
	getRootPanel: function(item) {
		var getContainer=function (obj) {
			if (obj.constructor==JSER.Panel)
				return obj;				
			else if (obj.container)
				return getContainer(obj.container);			
		};
		
		return getContainer(item);
	}	
};

JSER.Model.parse=function(container, items) {
	
	var list=[];
	
	for (var i=0; i<items.length; i++) {
		
		var item=items[i];
		
		if (item.label) {
			var labelItem=new Object();
			labelItem.xtype='label';
			labelItem.name=item.name;
			labelItem.text=item.label;
			labelItem.x=item.x;
			labelItem.y=item.y;
			list.push(new JSER.Model.Form.Items.Label(container, labelItem));				
		}
		
		switch (item.xtype) {
		case 'window':
			list.push(new JSER.Window(container, item));
			break;
		case 'panel':
			list.push(new JSER.Model.Panel(container, item));
			break;	
		case 'form':
			list.push(new JSER.Model.Form(container, item));
			break;	
		case 'grid':
		
			var module=JSER.Model.nav.getModule(container);
			var app=module.container;
			var grid=new JSER.Model.Grid(container, item);
			var store=(item.entity)? 
				app.storage.create(app.storage.getRecord(item.entity), grid).get()
				:
				module.store;
				
			if (store!==module.store)	
				module.storeList.push(store);
			
			grid.setStore(store);
			
			list.push(grid);
			break;
		case 'textfield':
			list.push(new JSER.Model.Form.Items.TextField(container, item));
			break;	
		case 'combobox':
		
			var module=JSER.Model.nav.getModule(container);
			var app=module.container;
			var combo=new JSER.Model.Form.Items.ComboBox(container, item);
			
			var store=app.storage.create(app.storage.getRecord(item.entity), combo).get();
			
			module.storeList.push(store);
			combo.setStore(store);
			
			list.push(combo);
			break;
		case 'hidden':
			list.push(new JSER.Model.Form.Items.Hidden(container, item));
			break;	
		default:		
			list.push(item);
			break;
		}
	}
	
	return list;
};

JSER.Model.render=function(item) {
	
	var render=function(obj) {
		
		var newObj=obj.get();
		newObj.container=obj.container;
		
		if (obj.items)			
			for (var i=0; i<obj.items.length; i++)				
				newObj.items[i]=render(obj.items[i]);
		
		return newObj;			
	}
	
	return render(item);
};

JSER.Model.Desktop=function() {
	
	this.moduleList=[];
	var scope=this;
	
	this.add=function(module) {			
		var modulePrototype=Ext.extend(Ext.app.Module, new JSER.Model.Desktop.Module(module).get());
		var newModule=new modulePrototype();
		this.moduleList.push(newModule);
	};
	
	this.get=function() {
		if (!this.obj)
			this.obj=new Ext.app.App({
				getModules : function(){
					return scope.moduleList;
				},
			    getStartConfig : function(){
			        return {
			            title: logged.User.name,
			            iconCls: 'user',
			            toolItems: [{
			                text:'Config.',
			                iconCls:'settings',
			                scope:this
			            },'-',{
			                text:'Logout',
			                iconCls:'logout',
			                scope:this,
			                handler: function () {
			                	window.location='login';
			                }
			            }]
			        };
			    }
			});
		return this.obj;	
	};
};

JSER.Model.Desktop.Module=function(module) {
	
	var scope=this;
	
	this.model=module;
		
	this.getId=function() {
		return 'desktop-module-'+this.model.name;
	};
	
	this.getTitle=function() {
		return this.model.title;
	};
	
	this.window=new JSER.Model.Window(this, {
		title: this.model.config.title,
		width: this.model.config.window.width,
		height: this.model.config.window.height,
		items: [this.model.view]
	}).get();
	
	this.loadStores=function() {
		for (var i=0; i<this.model.storeList.length; i++) 
			this.model.storeList[i].reload();
	};
	
	this.loadEvents=function() {
		this.model.loadEvents();
	};

	this.get=function() {
		if (!this.obj)
			this.obj={
			    id: this.getId(),
			    init : function() {
			        this.launcher = {
			            text: scope.getTitle(),
			            iconCls:'icon-grid',
			            handler : this.createWindow,
			            scope: this
			        }
			    },
			    createWindow : function() {
			        var desktop = this.app.getDesktop();
			        var win = desktop.getWindow(scope.getId());
			        
			        if(!win){
			            win = desktop.createWindow(scope.window); 	            
			            win.on({
			                'activate': {
			                	fn: function() {
			                    	scope.loadStores();	
			                    	scope.loadEvents();
			                    }
			                },
			                'close': {
			                	fn: function() {
			                		;
			                	}	
			                }});
			        }   
			        win.show();
			    }
			};
			
		return this.obj;	
	};
};

JSER.Model.Window=function(container, config) {
	
	var scope=this;
	
	this.container=container;
	this.config=config;
	
	this.items=null;
	
	this.getId=function() {
		this.id=this.config.name+'-win';
		return this.id;
	};
	
	this.getTitle=function() {
		this.title=this.config.title;
		return this.title;
	};
	
	this.getItems=function() {		
		this.items=this.config.items;
		return this.items;
	};
	
	this.getWidth=function() {
		this.width=(this.config.width)? this.config.width : 600;
		return this.width;
	};
	
	this.getHeight=function() {
		this.heigth=(this.config.height)? this.config.height : 600;
		return this.heigth; 
	};
	
	this.get=function() {
		if (!this.obj)
			this.obj={           		
		    	id: this.getId(),
		        title: this.getTitle(),
		        width: this.getWidth(),
		        height: this.getHeight(),
		        iconCls: 'icon-grid',
		        shim: false,
		        animCollapse: false,
		        constrainHeader: true,
		        resizable: true,
		        closeAction: 'close',
		        layout: 'fit',
		        items: this.getItems()
		    };
		    
		return this.obj; 
	};	
};

JSER.Model.Panel=function(container, config) {
	
	var that=this;
	
	this.container=container;
	this.config=config;
	
	this.getName=function() {
		if (!this.name)
			this.name=(this.config.name)? this.config.name : this.container.name+'-panel';
		
		return this.name;
	};
	
	this.getId=function() {
		if (!this.id)
			this.id=this.getName();
		return this.id;
	};	
	
	this.getItems=function() {
		if (!this.items)
			this.items=(this.config.items)? JSER.Model.parse(this, this.config.items) : [];
		
		return this.items;
	};
	
	this.getTbar=function() {
		return {
			xtype: 'toolbar',
	        items: [
	            {
	                xtype: 'buttongroup',
	                title: '',
	                columns: 5,
	                frame: false,
	                border: false,
	                items: [
	                    {
	                        xtype	: 'button',
	                        id		: this.getId()+'-button-save',
	                        text	: 'Salvar',
	                        width	: 60,
	                        iconCls	: 'save'
	                    },
	                    {
	                        xtype	: 'button',
	                        id		: this.getId()+'-button-new',
	                        text	: 'Novo',
	                        width	: 60,
	                        disabled: true,
	                        iconCls	: 'add'
	                    },
	                    {
	                        xtype	: 'button',
	                        id		: this.getId()+'-button-delete',
	                        text	: 'Deletar',
	                        width	: 60,
	                        disabled: true,
	                        iconCls	: 'remove'
	                    }
	                ]
	            }
	        ]
		}
	};
	
	this.getStatusBar=function() {
		 return this.statusBar=new Ext.ux.StatusBar({
			 id: this.getId()+'-statusbar',
	         defaultText: 'Ready'
	     });
	};
	
	this.get=function() {
		if (!this.obj)
			this.obj={
				xtype	: 'panel',
				id      : this.container.name+'-panel-'+this.config.name,
				name	: this.config.name,
			    tbar	: this.getTbar(),	    
			    bbar	: this.getStatusBar(),
				items	: this.getItems(),
				layout  : 'absolute',
				border	: false		
			};
		
		return this.obj;	
	};
};	

JSER.Model.Form=function(container, config) {
	
	var scope=this;
	
	this.container=container;
	this.config=config;	
	
	this.getName=function() {
		
		if (!this.name) {
			if (this.config.name)
				this.name=this.config.name;
			else (this.container.name)
				this.name=this.container.name+'-form';
		}
		
		return this.name;
	};
	
	this.getId=function() {
		if (!this.id)
			this.id=this.getName();
		
		return this.id;
	};	
	
	this.getItems=function() {
		if (!this.items)
			this.items=JSER.Model.parse(this, this.config.items);
		
		return this.items;
	};
	
	this.getStore=function() {
		if (!this.store) 
			this.store=(this.config.store)? this.config.store : this.getAppModule();
		
		return this.store;
	};
	
	this.getAppModule=function() {
		var module=this.getModule();
		return module.store;
	};
	
	this.getModule=function() {
		var getContainer=function (obj) {
			if (obj.xtype=='module')
				return obj;				
			else if (obj.container)
				return getContainer(obj.container);			
		};
		
		return getContainer(this);
	};
	
	this.getLinkedItem=function() {
		if (!this.linkedItem)
			this.linkedItem=(this.config.linkedItem)? this.config.linkedItem : null;
			
		return this.linkedItem
	};
	
	this.getHeight=function() {
		if (!this.height)
			this.height=(this.config.height)? this.config.height : 500;
		
		return this.height;
	};
	
	this.get=function() {
		if (!this.obj)
			this.obj={
				xtype		: 'form',	
				id			: this.getId(),
				name		: this.getName(),
			    frame		: false,
			    border		: false,
			    labelAlign	: 'left',
			    bodyStyle	: 'padding:5px',
			    defaultType	: 'textfield',
			    layout  	: 'absolute',
			    height		: this.getHeight(),
			    
			    store: this.getStore(),    
			    
			    items: this.getItems()
			};
		return this.obj;	
	}
};

JSER.Model.Form.Items={};

JSER.Model.Form.Items.TextField=function(container, config) {
	
	var scope=this;
	
	this.container=container;
	this.config=config;
	
	this.getY=function() {
		if (!this.y)
			this.y=(scope.config.label)? scope.config.y+15 : scope.config.y;
		
		return this.y;
	},	
	
	this.get=function() {
		if (!this.obj)
			this.obj={
				xtype	: 'textfield',
				id		: container.name+'-textfield-'+config.name,
				name	: config.name,
				x		: config.x,
				y		: scope.getY(),
				width	: config.width
			};
		return this.obj;		
	};
};

JSER.Model.Form.Items.Hidden=function(container, config) {
	
	this.container=container;
	this.config=config;
	
	this.get=function() {
		if (!this.obj)
			this.obj={
				xtype	: 'hidden',
				id      : container.name+'-hidden-'+config.name,
				name	: config.name
			};
		return this.obj;	
	};
};

JSER.Model.Form.Items.Label=function(container, config) {
	
	this.container=container;
	this.config=config;
	
	this.get=function() {
		if (!this.obj)
			this.obj={
				xtype	: 'label',
				id		: this.container.name+'-label-'+this.config.name,
				text	: this.config.text+":",
				x		: this.config.x,
				y		: this.config.y
			};
		
		return this.obj;	
	};
};

JSER.Model.Form.Items.ComboBox=function(container, config) {
	
	this.container=container;	
	this.config=config;	
	
	var that=this;
	
	this.getName=function() {
		this.name=this.config.name;			
		return this.name;	
	};
	
	this.getId=function() {
		if (!this.id) 
			this.id=(this.config.id)? this.config.id : this.id=this.container.name+'-combobox-'+this.config.name;
			
		return this.id;	
	};
	
	this.setStore=function(store) {		
		this.store=store;	
	};
	
	this.getStore=function() {
		if (this.store)
			return this.store;	
		return null;	
	};
	
	this.getValueField=function() {
		if (!this.valueField) 
			this.valueField=(this.config.valueField)? this.config.valueField : this.valueField='id';
			
		return this.valueField;	
	};

	this.getDisplayField=function() {
		if (!this.displayField) 
			this.displayField=(this.config.displayField)? this.config.displayField : this.displayField='name';
			
		return this.displayField;	
	};

	this.getMode=function() {
		if (!this.mode) 
			this.mode=(this.config.mode)? this.config.mode : this.mode='remote';
			
		return this.mode;	
	};
	
	this.getTypeAhead=function() {
		if (!this.typeAhead) 
			this.typeAhead=(this.config.typeAhead)? this.config.typeAhead : this.typeAhead='all';
			
		return this.typeAhead;	
	};
	
	this.getTriggerAction=function() {
		if (!this.triggerAction) 
			this.triggerAction=(this.config.triggerAction)? this.config.triggerAction : this.triggerAction='all';
			
		return this.triggerAction;	
	};
	
	this.getEmptyText=function() {
		if (!this.emptyText) 
			this.emptyText=(this.config.emptyText)? this.config.emptyText : this.emptyText='Selecione...';
			
		return this.emptyText;	
	};
	
	this.getSelectOnFocus=function() {
		if (!this.selectOnFocus) 
			this.selectOnFocus=(this.config.selectOnFocus)? this.config.selectOnFocus : this.selectOnFocus=true;
			
		return this.selectOnFocus;	
	};
	
	this.getWidth=function() {
		if (!this.width) 
			this.width=(this.config.width)? this.config.width : this.width=150;
			
		return this.width;	
	};
	
	this.getX=function() {		
		if (!this.x) 
			this.x=(this.config.x)? this.config.x : this.x=10;
			
		return this.x;			
	};
	
	this.getY=function() {
		if (!this.y)
			this.y=(this.config.label)? this.config.y+15 : this.config.y;
		
		return this.y;
	},	
	
	this.get=function() {
		if (!this.obj)
			this.obj=new Ext.form.ComboBox({
				id				: that.getId(),
				name			: that.getName(),
		        store			: that.getStore(),
		        valueField		: that.getValueField(),
		        displayField	: that.getDisplayField(),
		        typeAhead		: that.getTypeAhead(),
		        mode			: that.getMode(),
		        triggerAction	: that.getTriggerAction(),
		        emptyText		: that.getEmptyText(),
		        selectOnFocus	: that.getSelectOnFocus(),
		        width			: that.getWidth(),
		        x				: that.getX(),
		        y				: that.getY()
	   	 	});
	   	 	
	   	 return this.obj;	
	};
};

JSER.Model.Grid=function(container, config) {
	
	var that=this;
	
	this.container=container;
	this.config=config;
	
	this.getName=function() {
		if (!this.name)
			this.name=(this.config.name)? this.config.name : this.container.name+'-grid';
		
		return this.name;
	};
	
	this.getId=function() {
		return this.getName();
	};
	
	this.getTitle=function() {
		return (this.config.title)? this.config.title : 'Lista de '+this.getName();
	};
	
	this.setStore=function(store) {
		this.store=store;
	};
	
	this.getStore=function() {
		return this.store;
	};
	
	this.getX=function() {
		return (this.config.x)? this.config.x : 5;
	};
	
	this.getY=function() {
		if (this.config.y==undefined)
			throw ("Defina a posição y da grid "+this.config.name+".");
		
		return this.config.y;
	};

	this.getWidth=function() {
		return (this.config.width)? this.config.width : 576;
	};

	this.getHeight=function() {
		return (this.config.height)? this.config.height : 393;
	};	
	
	this.getBbar=function() {
		return new Ext.PagingToolbar({
		       pageSize		: PAGE_LIMIT,
		       store		: that.store,
		       displayInfo	: true,
		       displayMsg	: 'Exibindo registros {0} - {1} of {2}',
		       emptyMsg		: "Nenhum registro encontrado."
		});
	};
	
	this.getColModel=function() {
		return new Ext.grid.ColumnModel({
			defaults: {
				xtype		: 'gridcolumn',
	            width		: 120,
	            sortable	: true
	        },
	        columns: this.config.columns	
		});		
	};
	
	this.getSelModel=function() {
		return new Ext.grid.RowSelectionModel({
			singleSelect: true			
		});
	};
	
	this.get=function() {
		if (!this.obj)
			this.obj={	
				xtype		: 'grid',	
				title		: this.getTitle(),
			    id			: this.getId(),
			    x			: this.getX(),
			    y			: this.getY(),
			    width		: this.getWidth(),
			    height		: this.getHeight(),
			    border		: true,
			    store		: this.getStore(),
			    colModel	: this.getColModel(),
			    selModel	: this.getSelModel(),
			    bbar		: this.getBbar()		
			};
			
		return this.obj;	
	};
};
		
